<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="78px"
    >
      <el-form-item label="故障描述" prop="equipmentId">
        <el-input
          v-model="queryParams.faultDescribtion"
          placeholder="请输入故障描述"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col :span="5" style="padding-right:0 !important">
        <left-tree ref="leftRef" @getParentId="getParentIdFunc" @cateChange="cateChange" />
      </el-col>
      <el-col :span="19" style="padding-left:10 !important">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['equipment:faultAnalysis:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['equipment:faultAnalysis:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['equipment:faultAnalysis:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-download"
              size="mini"
              :loading="btnLoading"
              @click="handleExport"
              v-hasPermi="['equipment:faultAnalysis:export']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>

        <el-table
        :cell-style="{ padding: this.tablePadding }"
          v-loading="loading"
          border
          height="350"
          :data="analysisList"
          @selection-change="handleSelectionChange"
          @row-click="rowClick"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column
            label="故障分析ID"
            align="center"
            prop="faultAnalysisId"
          /> -->
          <el-table-column label="设备名称" align="center" prop="equipmentName" />
          <el-table-column
            label="报警类型"
            align="center"
            prop="alarmTypeName"
          />
          <el-table-column
            label="故障描述"
            align="center"
            prop="faultDescribtion"
          />
          <el-table-column
            label="故障分析"
            align="center"
            prop="faultAnalysis"
          />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['equipment:faultAnalysis:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['equipment:faultAnalysis:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
        <el-row style="padding:20px 0;">
          <el-tabs>
            <el-tab-pane label="解决方案">
              <solution-details :selectItems="solutionDetailsData"/>
            </el-tab-pane>
            <el-tab-pane label="文件列表">
              <file-details  :selectItems="fileDetailsData"/>
            </el-tab-pane>
          </el-tabs>
        </el-row>
      </el-col>
    </el-row>
    <!-- 右下布局 -->
        
    <!-- 添加或修改设备管理 故障分析对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form
        ref="form"
        :inline="true"
        layout="inline"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="故障分类" prop="alarmTypeId">
          <div style="width: 575px">
            <treeselect
              v-model="form.alarmTypeId"
              :options="categoryList"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
          </div>
        </el-form-item>
        <el-form-item label="设备" prop="equipmentId">
          <div class="input-width">
            <el-select
              v-model="form.equipmentId"
              placeholder="请选择设备"
              clearable
            >
              <el-option
                v-for="item in equipmentOptions"
                :key="item.equipmentId"
                :label="item.equipmentName"
                :value="item.equipmentId"
                :disabled="item.status==0"
              ></el-option>
            </el-select>
          </div>
        </el-form-item>
        <el-form-item label="故障描述" prop="faultDescribtion">
          <div style="width: 222px">
            <el-input
              v-model="form.faultDescribtion"
              type="textarea"
              placeholder="请输入内容"
            />
          </div>
        </el-form-item>
        <el-form-item label="故障分析" prop="faultAnalysis">
          <div style="width: 222px">
            <el-input
              v-model="form.faultAnalysis"
              type="textarea"
              placeholder="请输入内容"
            />
          </div>
        </el-form-item>
        <solution ref="solutionRef" :selectItems="solutionSelectItems"/>
        <div style="margin-top:15px">
            <file-list ref="fileRef" :selectItems="referenceSelectItems" />
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="btnLoading" v-funThrottle="[submitForm]">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAnalysis,
  getAnalysis,
  delAnalysis,
  addAnalysis,
  updateAnalysis,
  listCategory,
} from "@/api/equipment/faultAnalysis";
import { listInfo } from "@/api/equipment/info";
import LeftTree from "./components/Left.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Solution from './components/Solution.vue'//解决方案组件
import FileList from './components/File.vue'//添加参考资料组件
import SolutionDetails from './components/SolutionDetails.vue'
import FileDetails from './components/FileDetails.vue'
export default {
  name: "Analysis",
  components: { LeftTree, Treeselect,Solution,FileList,FileDetails,SolutionDetails },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备管理 故障分析表格数据
      analysisList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      solutionSelectItems:[],
      referenceSelectItems:[],
      solutionDetailsData:[],
      fileDetailsData:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        equipmentId: null,
        alarmTypeId: null,
        faultDescribtion: null,
        faultAnalysis: null,
      },
      // 表单参数
      form: {},
      equipmentOptions:[],
      btnLoading:false,
      categoryList:[],
      selectNames:[],
      // 表单校验
      rules: {
        equipmentId: [
          { required: true, message: "设备不能为空", trigger: "blur" },
        ],
        alarmTypeId: [
          { required: true, message: "故障类型不能为空", trigger: "blur" },
        ],
        faultDescribtion: [
          { required: true, message: "故障描述不能为空", trigger: "blur" },
        ],
        createBy: [
          { required: true, message: "建立人不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: true, message: "建立时间不能为空", trigger: "blur" },
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
      this.getEquipmentOptions()
  },
  methods: {
    rowClick(col){
       getAnalysis(col.faultAnalysisId).then((response) => {
        this.solutionDetailsData = response.data.equipmentSolutionList;
       this.fileDetailsData=response.data.equipmentReferenceList
      });
    },
    getEquipmentOptions() {
      listInfo().then((response) => {
        this.equipmentOptions = response.rows;
      });
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.alarmTypeId,
        label: node.alarmTypeName,
        children: node.children,
      };
    },
    cateChange() {
      this.getCategoryList();
    },
    getCategoryList() {
      listCategory().then((response) => {
        this.categoryList = response.data;
      });
    },
    getParentIdFunc(len, id) {
      if (len > 0) {
        this.queryParams.alarmTypeId = id;
        this.handleQuery();
      } else {
        this.loading = false;
      }
    },
    /** 查询设备管理 故障分析列表 */
    getList() {
      this.loading = true;
      listAnalysis(this.queryParams).then((response) => {
        this.analysisList = response.rows;
          this.solutionDetailsData=[]
          this.fileDetailsData=[]
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        faultAnalysisId: null,
        equipmentId: null,
        alarmTypeId: null,
        faultDescribtion: null,
        faultAnalysis: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.solutionSelectItems=[]
      this.referenceSelectItems=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.faultDescribtion=''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.faultAnalysisId);
      this.selectNames = selection.map((item) => item.faultDescribtion);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
       this.form.alarmTypeId=this.queryParams.alarmTypeId
      this.open = true;
      this.title = "添加故障分析";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const faultAnalysisId = row.faultAnalysisId || this.ids;
      getAnalysis(faultAnalysisId).then((response) => {
        this.form = response.data;
        this.solutionSelectItems=this.form.equipmentSolutionList
      this.referenceSelectItems=this.form.equipmentReferenceList
        this.open = true;
        this.title = "修改故障分析";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {

        if (valid) {
            this.form.equipmentSolutionList=this.$refs.solutionRef.selectItems;
            this.form.equipmentReferenceList=this.$refs.fileRef.selectItems;
            this.btnLoading=true
          if (this.form.faultAnalysisId != null) {
            updateAnalysis(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
             this.btnLoading=false
            }).catch(()=>{
              this.btnLoading=false
            });
          } else {
            addAnalysis(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
             this.btnLoading=false
              // 设置新增数据时选中的节点，重显到tree上
                this.$refs.leftRef.setNodeId(this.form.alarmTypeId);
            }).catch(()=>{
              this.btnLoading=false
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const faultAnalysisIds = row.faultAnalysisId || this.ids;
      const name=row.faultDescribtion||this.selectNames
      this.$confirm(
        '是否确认删除故障描述为"' +
          name +
          '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",closeOnClickModal:false,
          type: "warning",
        }
      )
        .then(function () {
          return delAnalysis(faultAnalysisIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.btnLoading=true
      this.download(
        "equipment/faultAnalysis/export",
        {
          ...this.queryParams,
        },
        `equipment_faultAnalysis.xlsx`,
        '',
        ()=>{
          this.btnLoading=false
        }
      );
    },
  },
};
</script>